package com.zoho.daliansheng.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zoho.daliansheng.common.U8SystemRecord;
import com.zoho.daliansheng.common.ZohoRecord;
import com.zoho.daliansheng.config.CRMPathConfig;
import com.zoho.daliansheng.config.DatasourceEnum;
import com.zoho.daliansheng.config.U8SystemPathConfig;
import com.zoho.daliansheng.dlcr.DlcrU8WebService;
import com.zoho.daliansheng.dlcr.DlcrU8WebServiceSoap;
import com.zoho.daliansheng.dlcr.QueryRetInfo;
import com.zoho.daliansheng.utils.CrmApiUtil;
import com.zoho.daliansheng.utils.getCrmFieldUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class FaHuoServiceImpl implements FaHuoService {

    ZohoRecord zohoRecord;

    U8SystemRecord u8SystemRecord;

    U8SystemPathConfig u8SystemPathConfig;

    CRMPathConfig crmPathConfig;

    DispatchListService dispatchListService;

    DispatchDetailListService detailListService;

    public FaHuoServiceImpl(ZohoRecord zohoRecord, U8SystemRecord u8SystemRecord, U8SystemPathConfig u8SystemPathConfig, CRMPathConfig crmPathConfig, DispatchListService dispatchListService, DispatchDetailListService detailListService) {
        this.zohoRecord = zohoRecord;
        this.u8SystemRecord = u8SystemRecord;
        this.u8SystemPathConfig = u8SystemPathConfig;
        this.crmPathConfig = crmPathConfig;
        this.dispatchListService = dispatchListService;
        this.detailListService = detailListService;
    }

    @Override
    public void synFaHuoList() {

        /*
         * 1 获取CRM 发货单 模块数据
         */

        // 客户
        List<String> tempFieldList = getCrmFieldUtil.getAccountFieldList();
        List<JSONObject> accountData = CrmApiUtil.getModuleData(zohoRecord.getAccessToken(), crmPathConfig.getAccountModuleName(), tempFieldList);

        // 所有用户
        List<JSONObject> allUserData = CrmApiUtil.getAllUserData(zohoRecord.getAccessToken());

        // 产品
        List<String> productFieldList = getCrmFieldUtil.getProductFieldList();
        List<JSONObject> productList = CrmApiUtil.getModuleData(zohoRecord.getAccessToken(), crmPathConfig.getProductModuleName(), productFieldList);

        // 产品仓库
        List<String> tempList = getCrmFieldUtil.getWarehouseFieldList();
        List<JSONObject> warehouseData = CrmApiUtil.getModuleData(zohoRecord.getAccessToken(), crmPathConfig.getWarehouseModuleName(), tempList);

        // 现存量
        List<String> xianList = getCrmFieldUtil.getCurrentStockFieldList();
        List<JSONObject> currentStockList = CrmApiUtil.getModuleData(zohoRecord.getAccessToken(), crmPathConfig.getCurrentStockModuleName(), xianList);


        try {

            /*
             * 2 同步ERP 发货单
             */
            synU8DataNew(DatasourceEnum.SU_ZHOU.getWebServiceUrl(), DatasourceEnum.SU_ZHOU.getAccountCode(), DatasourceEnum.SU_ZHOU.getCompanyName(), accountData, allUserData, productList, warehouseData, currentStockList);
            synU8DataNew(DatasourceEnum.CHANG_ZHOU.getWebServiceUrl(), DatasourceEnum.CHANG_ZHOU.getAccountCode(), DatasourceEnum.CHANG_ZHOU.getCompanyName(), accountData, allUserData, productList, warehouseData, currentStockList);
            synU8DataNew(DatasourceEnum.DA_LIAN.getWebServiceUrl(), DatasourceEnum.DA_LIAN.getAccountCode(), DatasourceEnum.DA_LIAN.getCompanyName(), accountData, allUserData, productList, warehouseData, currentStockList);
            synU8DataNew(DatasourceEnum.XIN_CAI_LIAO.getWebServiceUrl(), DatasourceEnum.XIN_CAI_LIAO.getAccountCode(), DatasourceEnum.XIN_CAI_LIAO.getCompanyName(), accountData, allUserData, productList, warehouseData, currentStockList);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void synU8DataNew(String webServiceUrl, String accountCode, String companyName, List<JSONObject> accountData, List<JSONObject> allUserData,
                              List<JSONObject> productList, List<JSONObject> warehouseData, List<JSONObject> currentStockList) {
        log.info("同步【" + companyName + "】");
        /*
         * 1 先获取 ERP的数据
         */
        try {
            DlcrU8WebService webService = new DlcrU8WebService(new URL(webServiceUrl));
            DlcrU8WebServiceSoap api = webService.getDlcrU8WebServiceSoap();

            String head_synModuleName = crmPathConfig.getDispatchModuleName(); // 要同步的模块名称
            Map<String, String> head_map = CrmApiUtil.getMaxVersion(zohoRecord.getAccessToken(), head_synModuleName, accountCode);
            String head_versionCrmId = head_map.get("id");
            int head_maxVersion = Integer.parseInt(head_map.get("maxVersion"));

            String detailModuleName = crmPathConfig.getDispatchDetailModuleName(); // 要同步的模块名称  发货单明细
            Map<String, String> body_map = CrmApiUtil.getMaxVersion(zohoRecord.getAccessToken(), detailModuleName, accountCode);
            String body_versionCrmId = body_map.get("id");
            int body_maxVersion = Integer.parseInt(body_map.get("maxVersion"));


            // ERP 发货单数据
            log.info("===========发货单============");
            log.info("head_maxVersion={}", head_maxVersion);
            QueryRetInfo head_queryRetInfo = api.queryDispatchListByVersion(head_maxVersion);
            int head_queryMaxVersion = head_queryRetInfo.getMaxVersion();
            JSONArray head_jsonArray = JSONArray.parseArray(head_queryRetInfo.getJson());
            List<JSONObject> head_erpList = new ArrayList<>();
            if (head_jsonArray.size() > 0) {
                head_erpList = head_jsonArray.toJavaList(JSONObject.class);
                log.info("ERP发货单数据={}", JSONObject.toJSONString(head_erpList));
            }

            // ERP 发货单明细数据
            log.info("===========发货单明细============");
            log.info("body_maxVersion={}", body_maxVersion);
            QueryRetInfo body_queryRetInfo = api.queryDispatchListsByVersion(body_maxVersion);
            int body_queryMaxVersion = body_queryRetInfo.getMaxVersion();
            JSONArray body_jsonArray = JSONArray.parseArray(body_queryRetInfo.getJson());
            List<JSONObject> body_erpList = new ArrayList<>();
            if (body_jsonArray.size() > 0) {
                body_erpList = body_jsonArray.toJavaList(JSONObject.class);
                log.info("ERP发货单明细数据={}", JSONObject.toJSONString(body_erpList));
            }
             log.info("-----开始【CRM销售发货单模块】-----");
            dispatchListService.synU8Data(webServiceUrl, accountCode, companyName, accountData, allUserData, head_maxVersion, head_versionCrmId, head_erpList, head_queryMaxVersion);
             log.info("-----结束【CRM销售发货单模块】-----");
             log.info("-----开始【CRM销售发货单明细模块】-----");
            detailListService.synU8Data(webServiceUrl, accountCode, companyName, productList, warehouseData, currentStockList, body_maxVersion, body_queryMaxVersion, body_erpList, body_versionCrmId);
             log.info("-----结束【CRM销售发货单明细模块】-----");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

